import redis
import json

from log import MyLogging

log = MyLogging()


class RedisBiliBili(object):
    def __init__(self):
        self.R0 = redis.Redis(host="127.0.0.1", port=6379)
        # self.R1 = redis.Redis(host="127.0.0.1", port=6379, db=1)
        # self.R2 = redis.Redis(host="127.0.0.1", port=6379, db=2)

    def set_msg(self, data):
        try:
            self.R0.lpush('bilibili', json.dumps(data))
            log.write_logger(f'{data["title"]} 成功写入消息')
        except Exception as e:
            log.error_logger(str(e))

    def get_msg(self):
        try:
            data = self.R0.lpop('bilibili')
            if data:
                return json.loads(data)
            else:
                return False
        except Exception as e:
            log.error_logger(str(e))


if __name__ == '__main__':
    R = RedisBiliBili()
    while True:
        print(R.get_msg())
    # def get_title(self, title):
    #     if self.R1.get(title):
    #         return True
    #     else:
    #         self.R1.setex(title, 259200, '1')
    #         return False
    #
    # def get_data(self, _id):
    #     if self.R2.get(_id):
    #         log.write_logger(f"_id：{_id} 已存在mongo中")
    #         return True
    #     else:
    #         self.R2.setex(_id, 259200, '1')
    #         return False
